#include "qvarianttablemodel.h"

QVariantTableModel::QVariantTableModel(QObject *parent) :
    QAbstractTableModel(parent), iColumnCount(0)
{
}

bool QVariantTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
    if ( section >= iColumnCount )
        return false;

    QVariantMap* qvPtr = 0;
    if ( orientation == Qt::Horizontal )
        qvPtr = &qvHorizHeaderData;
    else
        qvPtr = &qvVertHeaderData;

    QString qsSection = QString::number(section);
    QString qsRole = QString::number(role);

    if ( !qvPtr->contains(qsSection) )
    {
        // AGGIUNGI NUOVA MAPPA
        QVariantMap qvMap;
        qvMap.insert(qsRole, value);
        qvPtr->insert(qsSection, qvMap);
    }
    else
    {
        QVariantMap qvMap = qvPtr->value(qsSection).toMap();
        qvMap.insert(qsRole, value);
        qvPtr->insert(qsSection, qvMap);
    }

    return true;
}

QVariant QVariantTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if ( section >= iColumnCount )
        return QVariant();

    QVariant qvHd;
    QString qsSection = QString::number(section);
    QString qsRole = QString::number(role);

    if ( orientation == Qt::Horizontal )
        qvHd = qvHorizHeaderData.value(qsSection);
    else
        qvHd = qvVertHeaderData.value(qsSection);

    if ( role == Qt::DisplayRole )
    {


        return qvHd.toMap().value(qsRole);
    }
    else if ( role == Qt::SizeHintRole )
    {
        //double stretch = qvHd.toMap().value("Stretch");
    }

    return QVariant();
}

int QVariantTableModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return qvData.count();
}

void QVariantTableModel::setColumnCount(int count)
{    
    iColumnCount = count;
}

int QVariantTableModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return iColumnCount;
}

QVariant QVariantTableModel::data(const QModelIndex &index, int role) const
{
    if ( role == Qt::DisplayRole || role == Qt::EditRole )
    {
        QString qsRole = QString::number(role);
        QString qsColumn = QString::number(index.column());

        QVariant qvRow = qvData.at(index.row()); // RIGA
        //QVariant qvElem = qvRow.toMap().value(qsColumn); // CELLA
        //QVariant qvValue = qvElem.toMap().value(qsRole); // VALORE

        return qvRow.toMap().value(qsColumn);
    }
    return QVariant();
}

bool QVariantTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if ( role == Qt::DisplayRole || Qt::EditRole )
    {
        //QString qsRole = QString::number(role);
        QString qsColumn = QString::number(index.column());

        QVariantMap qvRow = qvData.at(index.row()).toMap(); // RIGA
        //QVariantMap qvElem = qvRow.value(qsColumn).toMap(); // CELLA
        //qvElem.insert(qsRole, value);

        // REPLACE
        qvRow.insert(qsColumn, value);
        qvData.replace(index.row(), qvRow);
    }

    emit dataChanged(index, index);

    return true;
}

Qt::ItemFlags QVariantTableModel::flags(const QModelIndex &index) const
{
    if ( index.isValid() )
        return Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    return Qt::NoItemFlags;
}

bool QVariantTableModel::insertRows(int row, int count, const QModelIndex &parent)
{
    beginInsertRows(parent, row, row+count-1);
    QVariantMap qvMap;
    for( int j = 0; j < iColumnCount; ++j )
    {
        QString qsColumn = QString::number(j);
        qvMap.insert(qsColumn, QVariantMap());
    }

    for( int i = 0; i < count; ++i )
    {
        qvData.append(qvMap);
    }
    endInsertRows();

    return true;
}

bool QVariantTableModel::appendRow(const QVariantList & value)
{
    int row = qvData.count();
    if ( insertRow(row) )
    {
        for ( int i = 0; i < iColumnCount; ++i )
        {
            if ( value.count() == i )
                break;

            setData(
                    index(row, i),
                    value.at(i),
                    Qt::DisplayRole);
        }

        return true;
    }

    return false;
}

QVariantList QVariantTableModel::toVariantList() const
{
    return qvData;
}

void QVariantTableModel::importData(QVariant data)
{
    QList<QVariant> qvList = data.toList();
    beginInsertRows(QModelIndex(), 0, qvList.count()-1);
    qvData = qvList;
    endInsertRows();
}
